import { mapState } from 'vuex';
import { updateNumberCart,deleteCart,clearCart,listCart } from '@/api/cart.js';
	
export default {
	data() {
		return {
			total: 0, //总价格
			allChecked: true, //全选状态  true|false
			empty: false, //空白页现实  true|false
			//右侧滑动
			options:[{
				text: '删除',
				style: {
					backgroundColor: '#dd524d'
				}
			}],
			cartList: [],
		}
	},
	computed: {
		...mapState(['hasLogin'])
	},
	onShow() {
		this.loadData();
	},
	//下拉刷新
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	watch: {
		//显示空白页
		cartList(e) {
			let empty = e.length === 0 ? true : false;
			if (this.empty !== empty) {
				this.empty = empty;
			}
		}
	},
	methods: {
		//加载购物车
		async loadData(type = 'add') {
			if(!this.hasLogin){
				return;
			}
			listCart().then(response => {
				let list = response.data;
				let cartList = list.map(item => {
					item.checked = true;
					
					return item;
				});
				this.cartList = cartList;
				this.calcTotal(); //计算总价
				
				if (type === 'refresh') {
					uni.stopPullDownRefresh();
				}
			});
		},
		
		//滑动删除
		swipeClick(e, index) {
			let {
				content
			} = e;
			var _this = this;
			if (content.text === '删除') {
				uni.showModal({
					title: '提示',
					content: '是否取消删除',
					success: res => {
						if (res.confirm) {
							var select = this.cartList[index];
							let data = {
								id: select.id
							}
							deleteCart(data).then(response=>{
								_this.loadData('refresh');
							});
						
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			} else {
				uni.showToast({
					title: `点击了${e.content.text}按钮`,
					icon: 'none'
				});
			}
		},
		
		clearCart() {
			var _this = this;
			if (this.cartList.length != 0) {
				uni.showModal({
					title: '提示',
					content: '是否要清空购物车？',
					success: function (res) {
						if (res.confirm) {
							clearCart().then(response=>{
								_this.loadData('refresh');
							});
						}
					}
				});
			}
		},
		
		//选中状态处理
		check(index) {
			this.cartList[index].checked = !this.cartList[index].checked;
			this.calcTotal();
		},
		//选中所有状态处理
		checkAll(){
			const checked = !this.allChecked
			this.cartList.forEach(item => {
				item.checked = checked;
			})
			this.allChecked = checked;
			this.calcTotal();
		},
		//数量
		numberChange(numberData) {
			let cartItem = this.cartList[numberData.index];
			let data = {
				id: cartItem.id,
				number: numberData.number
			}
			updateNumberCart(data).then(response=>{
				this.cartList[numberData.index].number = numberData.number;
				this.calcTotal();
			});
		},
		numberBlur(e) {
			console.log('blur:', e);
		},
		numberFocus(e) {
			console.log('focus:', e);
		},
		//计算总价
		calcTotal() {
			let list = this.cartList;
			if (list.length === 0) {
				this.empty = true;
				return;
			}
			let total = 0;
			let checked = true;
			list.forEach(item => {
				if (item.checked === true) {
					total += item.price * item.number;
				} else {
					checked = false;
				}
			})
			this.allChecked = checked;
			this.total = Number(total.toFixed(2));
		},
		//去结算
		createOrder() {
			let list = this.cartList;
			let skuCodes = [];
			list.forEach(item => {
				if (item.checked) {
					skuCodes.push(item.skuCode);
				}
			})
			if(skuCodes.length==0){
				uni.showToast({
					title:'您还未选择要下单的商品！',
					duration:1000
				})
				return;
			}
			uni.navigateTo({
				url: `/pages/order/order?skuCodes=${JSON.stringify(skuCodes)}`
			})
		},
		
		//详情
		navToDetailPage(item) {
			uni.navigateTo({
				url: `/pages/product/product?sku=${item.sku}`
			})
		},
		
	}
}